import sys
import os

from multiprocessing import Process, Queue, Pool

import time
import thread

def daemonize():
    try:
        pid = os.fork()
        if pid > 0:
            sys.exit(0)
    except OSError:
        print "fork() failed"
        sys.exit(1)

    os.chdir("/")
    os.setsid()
    # Make sure I can read my own files and shut out others
    #prev= os.umask(0)
    #os.umask(prev and int('077',8))
    os.umask(0)
    
    try:
        pid = os.fork()
        if pid > 0:
            sys.exit(0)
    except OSError:
        print "fork() failed"
        sys.exit(1)

    dev_null = file('/dev/null', 'r')
    os.dup2(dev_null.fileno(), sys.stdin.fileno())


def do_work(buildObject):
    try:
        print "(%s) Zzzzzz" % buildObject
        
        if buildObject == 0:
            print "hum"
            time.sleep(25)
        else:
            time.sleep(10+buildObject)
        print "(%s) Done!" % buildObject
    except Exception, e:
        print "Big effing error: ", e
    
    return True

def main():
    q = Queue()

    q.put(1)
    q.put(2)
    q.put(3)
    q.put(4)

    print q.empty
        
    for worker in xrange(4):
        print "kicking off %s" % worker
        p = Process(name=worker, target=do_work, args=(q.get(),))
        p.start()
        

    print "p: %s" % p.name
if __name__ == '__main__':
    main()
    